﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class CategoryDetail
    {

        public CategoryDetail() { }

        #region [Get all value from package, category and detail]
        /// <summary>
        /// Get all value from package, category and detail 
        /// </summary>
        /// <returns>
        ///<para>value of list from data includes 3 table Category Detail + Category + Category Title</para>
        ///<para>null    : error when select Category Detail + Category + Category Title</para>
        /// </returns>
        public List<PC_CategoryDetails_getAllResult> getAll()
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_CategoryDetails_getAll().ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get one Category Detail]
        /// <summary>
        /// Get one Category Detail
        /// </summary>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <returns>
        /// <para>Return one Category Detail</para>
        /// <para>null  : error</para>
        /// </returns>
        public List<PC_CategoryDetails_getOneResult> getOne(string cId, string ctId)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_CategoryDetails_getOne(Int32.Parse(cId), Int32.Parse(ctId)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert category detail]
        /// <summary>
        /// Insert category detail
        /// </summary>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <param name="content">A description about Category and Title</param>
        /// <param name="active">Visible/Hidden on interface</param>
        /// <returns>
        /// <para>0    : successfully</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int add(string cId, string ctId, string content, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_CategoryDetails_Ins(Int32.Parse(cId), Int32.Parse(ctId), content, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert is valid or not]
        /// <summary>
        /// Check insert is valid or not
        /// </summary>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <param name="content">A description about Category and Title</param>
        /// <returns>
        /// <para>1    : successfully</para>
        /// <para>2    : Category ID is null or empty</para>
        /// <para>3    : Category ID is not valid</para>
        /// <para>4    : Category Title ID is null or epmty</para>
        /// <para>5    : Category Title is not valid</para>
        /// <para>6    : Content is greater than 2000 characters</para>
        /// <para>7    : Category ID and Category Title ID is exists in Category Detail</para>
        /// </returns>
        public int addValid(string cId, string ctId, string content)
        {
            if (String.IsNullOrEmpty(cId))
            {
                return 2;
            }

            Regex rcId = new Regex("^[0-9]{1,}$");
            Match mcId = rcId.Match(cId);
            if (!mcId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(ctId))
            {
                return 4;
            }

            Regex rctId = new Regex("^[0-9]{1,}$");
            Match mctId = rctId.Match(ctId);
            if (!mctId.Success)
            {
                return 5;
            }
            else if (content.Length > 2000)
            {
                return 6;
            }

            var list = getOne(cId, ctId);
            if (list.Count > 0)
            {
                return 7;
            }

            return 1;
        }
        #endregion

        #region [Edit category detail]
        /// <summary>
        /// Edit category detail
        /// </summary>
        /// <param name="oldCId">Old ID of Service Category</param>
        /// <param name="oldCTId">Old ID of Service Category Title</param>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <param name="content">A description about Category and Title</param>
        /// <param name="active">Visible/Hidden on interface</param>
        /// <returns>
        /// <para>0    : successfully</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int edit(string oldCId, string oldCTId, string cId, string ctId, string content, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_CategoryDetails_Upd(Int32.Parse(oldCId), Int32.Parse(oldCTId), Int32.Parse(cId), Int32.Parse(ctId), content, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check edit is valid or not]
        /// <summary>
        /// Check edit is valid or not
        /// </summary>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <param name="content">A description about Category and Title</param>
        /// <returns>
        /// <para>1    : successfully</para>
        /// <para>2    : Category ID is null or empty</para>
        /// <para>3    : Category ID is not valid</para>
        /// <para>4    : Category Title ID is null or epmty</para>
        /// <para>5    : Category Title is not valid</para>
        /// <para>6    : Content is greater than 2000 characters</para>
        /// </returns>
        public int editValid(string cId, string ctId, string content)
        {
            if (String.IsNullOrEmpty(cId))
            {
                return 2;
            }

            Regex rcId = new Regex("^[0-9]{1,}$");
            Match mcId = rcId.Match(cId);
            if (!mcId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(ctId))
            {
                return 4;
            }

            Regex rctId = new Regex("^[0-9]{1,}$");
            Match mctId = rctId.Match(ctId);
            if (!mctId.Success)
            {
                return 5;
            }
            else if (content.Length > 2000)
            {
                return 6;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Category Detail]
        /// <summary>
        /// Delete an existed Category Detail
        /// </summary>
        /// <param name="cId">ID of Service Category</param>
        /// <param name="ctId">ID of Service Category Title</param>
        /// <returns>
        /// <para>0     : successfully</para>
        /// <para>-99:  : error</para>
        /// </returns>
        public int delete(string cId, string ctId)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelte = db.PC_CategoryDetails_Del(Int32.Parse(cId), Int32.Parse(ctId));
                return isDelte;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}